package com.qianfeng.wuyang.himalaya.adapter.broadcastadapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qianfeng.wuyang.himalaya.R;
import com.qianfeng.wuyang.himalaya.bean.broadcastbean.BC_categoriesEntity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by wuyang on 2016/12/5.
 */

public class BCGridRecyclerAdapter extends RecyclerView.Adapter<BCGridRecyclerAdapter.ViewHolder> implements View.OnClickListener {
    private Context context;
    private List<BC_categoriesEntity> list;
    private List<BC_categoriesEntity> closeList;
    private boolean isOpen;
    public BCGridRecyclerAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
        closeList = new ArrayList<>();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder holder = new ViewHolder(LayoutInflater.from(context).inflate(viewType, parent, false));
        if (holder.image_gridRecycler !=null) {
            holder.image_gridRecycler.setOnClickListener(this);
        }

        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //得到每一个item对象
//        BC_categoriesEntity items = closeList.get(position);

        if (getItemViewType(position) != R.layout.itemimage_gridrecycler_bc) {
            holder.tv_gridRecycler.setText(closeList.get(position).getName());
        }else if(isOpen){
            holder.image_gridRecycler.setImageResource(R.mipmap.ic_triangle_up);
        }


//        holder.image_gridRecycler.setImageResource(R.mipmap.ic_more_normal);

//            holder.image_gridRecycler.setImageResource(R.mipmap.ic_more_normal);

    }

    @Override
    public int getItemCount() {
        //判断如果有数据则留出一个位置，没有数据则不留位置
        return closeList.size() == 0?closeList.size():closeList.size()+1;
    }

    @Override
    public int getItemViewType(int position) {
        //多布局的判断条件是什么？
        if (closeList.size() !=0 && position == closeList.size()) {
            return R.layout.itemimage_gridrecycler_bc;
        }else{
            return R.layout.item_gridrecycler_bc;
        }

    }

    //添加数据的方法
    public void addAll(Collection<? extends BC_categoriesEntity> collection){
        int size = closeList.size();
        list.addAll(collection);
        for (int i = 0; i < 7; i++) {
            closeList.add(list.get(i));
        }
        notifyItemRangeInserted(size,closeList.size()+1);
    }
    //点击按钮显示数据
    public void showData(){
        closeList.removeAll(closeList);
        closeList.addAll(list);
        notifyDataSetChanged();
    }
    //点击按钮隐藏数据
    public void hideData(){
        closeList.removeAll(closeList);
        for (int i = 0; i < 7; i++) {
            closeList.add(list.get(i));
        }
        notifyDataSetChanged();
    }

    //图片的监听
    @Override
    public void onClick(View view) {
        if (!isOpen) {
            showData();
            isOpen = true;
        }else if(isOpen){
            ((ImageView)view).setImageResource(R.mipmap.ic_triangle_down);
            hideData();
            isOpen=false;
        }
    }


    public static   class ViewHolder extends RecyclerView.ViewHolder{

        private TextView tv_gridRecycler;
        private ImageView image_gridRecycler;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_gridRecycler = ((TextView) itemView.findViewById(R.id.tx_gridRecycler));
            image_gridRecycler = ((ImageView) itemView.findViewById(R.id.image_gridRecycler));
        }
    }
}
